//
//  EnumController.swift
//  HHSwift
//
//  Created by 华宏 on 2019/4/24.
//  Copyright © 2019年 huahong. All rights reserved.
//

import UIKit

//值类型，赋值即是拷贝，产生一个副本，两者不关联
// enum第一个字母小写
enum OrientationA {
    case east
    case south
    case west
    case north
    
    //里面还可以定义方法
    func orientationFunc()  {
        print("实例方法") 
    }
   
  static  func orientationFunc1()  {
        print("类方法")
    }
    
}

enum OrientationB {
    case east,south,west,north
   
}

enum OrientationC:Int {
    case east = 1
    case south = 3
    case west = 5
    case north = 7
}

//第一项的rawValue默认为0，向下依次+1
enum OrientationD:Int {
    case east = 1
    case south
    case west
    case north
}

//如果不写后面的字符串，则rawValue默认为该枚举值
enum OrientationE:String {
    case east = "East"
    case south = "South"
    case west = "West"
    case north
}

//关联值
enum ATMStatus {
    case success(Int)
    case error(errMessage:String,errorCode:Int)
    case waiting
}

class EnumController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "枚举"

        
        _ = OrientationA.east
        let _:OrientationB = .east
        _ = OrientationC(rawValue: 3)
        let orientation5 = OrientationE.north
        print("orientation5 is \(orientation5)")
        
        
        //关联值使用
        let result = getMoney(amount: 1100)
        switch result {
        case let .success(balance):
            print("balance:\(balance)")
        case let .error(errMesg,code):
            print("error:\(errMesg),\(code)")
        default:
            print("waiting")
        }
        
        //使用关联值枚举创建对象
        let error = ATMStatus.error(errMessage: "message", errorCode: 1)
        
        
    }
    
//  取钱
    func getMoney(amount:Int) -> ATMStatus {
        var balance = 1000
        balance -= amount
        if balance >= amount {
            return .success(balance)
        }else
        {
            return .error(errMessage: "余额不足", errorCode: 1)
        }
        
    }
    
}
